home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / MiscKit1.7.1 / MiscKit / Palettes / MiscTableScroll / MiscTableFocus.M < prev    next >
Encoding:
Text File  |  1996-02-11  |  4.7 KB  |  176 lines

  1. //=============================================================================
  2. //
  3. //        Copyright (C) 1995 by Paul S. McCarthy and Eric Sunshine.
  4. //                Written by Paul S. McCarthy and Eric Sunshine.
  5. //                            All Rights Reserved.
  6. //
  7. //        This notice may not be removed from this source code.
  8. //
  9. //        This object is included in the MiscKit by permission from the authors
  10. //        and its use is governed by the MiscKit license, found in the file
  11. //        "License.rtf" in the MiscKit distribution.    Please refer to that file
  12. //        for a list of all applicable permissions and restrictions.
  13. //        
  14. //=============================================================================
  15. //-----------------------------------------------------------------------------
  16. // MiscTableFocus.M
  17. //
  18. //        Blinking "cursor" for showing keyboard focus in TableView.
  19. //
  20. //-----------------------------------------------------------------------------
  21. //-----------------------------------------------------------------------------
  22. // $Id: MiscTableFocus.M,v 1.1 95/09/27 12:21:21 zarnuk Exp $
  23. // $Log:        MiscTableFocus.M,v $
  24. //    Revision 1.1  95/09/27    12:21:21  zarnuk
  25. //    Initial revision
  26. //    
  27. //-----------------------------------------------------------------------------
  28.  
  29. #import "MiscTableFocus.h"
  30.  
  31. extern "Objective-C" {
  32. #import <appkit/Application.h>
  33. #import <appkit/View.h>
  34. }
  35. extern "C" {
  36. #import "MiscTableFocusPS.h"
  37. }
  38.  
  39. double const BLINK = 0.4;        // seconds
  40. enum { FOCUS_BLACK = NO, FOCUS_WHITE = YES };
  41.  
  42.  
  43. @implementation MiscTableFocus
  44.  
  45. //-----------------------------------------------------------------------------
  46. // initOwner:
  47. //-----------------------------------------------------------------------------
  48. - initOwner: (View*) o
  49.     {
  50.     [super init];
  51.     owner = o;
  52.     [owner getBounds: &frame];
  53.     state = FOCUS_BLACK;
  54.     return self;
  55.     }
  56.  
  57.  
  58. //-----------------------------------------------------------------------------
  59. // free
  60. //-----------------------------------------------------------------------------
  61. - free
  62.     {
  63.     if (timer != 0)
  64.         DPSRemoveTimedEntry( timer );
  65.     return [super free];
  66.     }
  67.  
  68.  
  69. //-----------------------------------------------------------------------------
  70. // canDraw
  71. //-----------------------------------------------------------------------------
  72. - (BOOL) canDraw
  73.     {
  74.     return ([self isShowingFocus] && frame.size.width > 0 &&
  75.                 frame.size.height > 0 && [owner canDraw]);
  76.     }
  77.  
  78.  
  79. //-----------------------------------------------------------------------------
  80. // drawFocus
  81. //-----------------------------------------------------------------------------
  82. - (void) drawFocus
  83.     {
  84.     if ([self canDraw])
  85.         {
  86.         BOOL needFocus = (![owner isFocusView]);
  87.         if (needFocus) [owner lockFocus];
  88.         MISC_TF_drawBox( frame.origin.x, frame.origin.y,
  89.                          frame.size.width, frame.size.height,
  90.                          state ? NX_WHITE : NX_BLACK );
  91.         if (needFocus) [owner unlockFocus];
  92.         }
  93.     }
  94.  
  95.  
  96. //-----------------------------------------------------------------------------
  97. // eraseFocus
  98. //-----------------------------------------------------------------------------
  99. - (void) eraseFocus
  100.     {
  101.     if ([self canDraw])
  102.         {
  103.         BOOL needFocus = (![owner isFocusView]);
  104.         if (needFocus) [owner lockFocus];
  105.         MISC_TF_eraseBox( frame.origin.x, frame.origin.y,
  106.                           frame.size.width, frame.size.height );
  107.         if (needFocus) [owner unlockFocus];
  108.         }
  109.     }
  110.  
  111.  
  112. //-----------------------------------------------------------------------------
  113. // setFrame:
  114. //-----------------------------------------------------------------------------
  115. - setFrame: (NXRect const*) r
  116.     {
  117.     [self eraseFocus];
  118.     frame = *r;
  119.     [self drawFocus];
  120.     return self;
  121.     }
  122.  
  123.  
  124. //-----------------------------------------------------------------------------
  125. // blinker
  126. //-----------------------------------------------------------------------------
  127. - (void) blinker
  128.     {
  129.     state = !state;
  130.     [self drawFocus];
  131.     }
  132.  
  133. static void blinker( DPSTimedEntry te, double now, void* userData )
  134.     {
  135.     [id(userData) blinker];
  136.     }
  137.  
  138.  
  139. //-----------------------------------------------------------------------------
  140. // startFocus
  141. //-----------------------------------------------------------------------------
  142. - (void) startFocus
  143.     {
  144.     if (![self isShowingFocus])
  145.         {
  146.         timer = DPSAddTimedEntry( BLINK, blinker, self, NX_RUNMODALTHRESHOLD );
  147.         state = FOCUS_BLACK;
  148.         [self drawFocus];
  149.         }
  150.     }
  151.  
  152.  
  153. //-----------------------------------------------------------------------------
  154. // stopFocus
  155. //-----------------------------------------------------------------------------
  156. - (void) stopFocus
  157.     {
  158.     if ([self isShowingFocus])
  159.         {
  160.         [self eraseFocus];
  161.         DPSRemoveTimedEntry( timer );
  162.         timer = 0;
  163.         }
  164.     }
  165.  
  166.  
  167. //-----------------------------------------------------------------------------
  168. // isShowingFocus
  169. //-----------------------------------------------------------------------------
  170. - (BOOL) isShowingFocus
  171.     {
  172.     return (timer != 0);
  173.     }
  174.  
  175. @end
  176.